package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yxx.annotation.OptLogger;
import com.yxx.annotation.VisitLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.FriendQuery;
import com.yxx.entityModel.vo.request.FriendRequest;
import com.yxx.entityModel.vo.response.FriendBackResponse;
import com.yxx.entityModel.vo.response.FriendResponse;
import com.yxx.service.FriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 友链 控制层
 * @author yuxiangxun
 * @date 2025/1/31
 * @apiNote
 */
@RestController
@Api(tags = "友链模块")
public class FriendController {
    @Resource
    private FriendService friendService;

    /**
     * 查看友链列表
     * @return
     */
    @GetMapping("/friend/list")
    @VisitLogger(value = "友链")
    @ApiOperation(value = "查看友链列表")
    public ResponseResult<List<FriendResponse>> FriendListVO(){
        return ResponseResult.success(friendService.friendListVO());
    }

    /**
     * 查看友链后台列表
     * @param friendQuery 查询条件
     * @return 后台好友列表
     */
    @ApiOperation(value = "查看友链后台列表")
    @SaCheckPermission("web:friend:list")
    @GetMapping("/admin/friend/list")
    public ResponseResult<PageResult<FriendBackResponse>> FriendBackListVO(FriendQuery friendQuery){
        return ResponseResult.success(friendService.friendListBackVO(friendQuery));
    }

    /**
     * 添加友链
     * @param friendRequest
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加友链")
    @SaCheckPermission("web:friend:add")
    @PostMapping("/admin/friend/add")
    public ResponseResult<?> addFriend(@RequestBody @Validated FriendRequest friendRequest){
        friendService.addFriend(friendRequest);
        return ResponseResult.success();
    }

    /**
     * 删除友链
     * @param friendIdList
     * @return
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除友链")
    @SaCheckPermission("web:friend:delete")
    @DeleteMapping("/admin/friend/delete")
    public ResponseResult<?> deleteFriend(@RequestBody List<Integer> friendIdList){
        boolean b = friendService.removeByIds(friendIdList);
        return ResponseResult.success(b);
    }

    /**
     * 修改友链
     * @param friendRequest
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改友链")
    @SaCheckPermission("web:friend:update")
    @PutMapping("/admin/friend/update")
    public ResponseResult<?> updateFriend(@Validated @RequestBody FriendRequest friendRequest){
        friendService.updateFriend(friendRequest);
        return ResponseResult.success();
    }

}
